/*******************************************/
/** ENTREPRENEUR - EXCLUDED - CSS problem **/
/*******************************************/



double focEhat(const double xval, void * params){
    
    struct paramsgoldenRB *focparams= (struct paramsgoldenRB *) params;
    
    double *expE_WWNE_JNE = focparams->expE_WWNE_JNE_ST;
    double *expE_WWE_JE   = focparams->expE_WWE_JE_ST;
    double *expE_UnNE_JNE = focparams->expE_UnNE_JNE_ST;
    double *expE_UnE_JE   = focparams->expE_UnE_JE_ST;
    
#if OPT_compute_C_flow == 1
    double *C_expE_WWNE_JNE = focparams->C_expE_WWNE_JNE_ST;
    double *C_expE_WWE_JE   = focparams->C_expE_WWE_JE_ST;
    double *C_expE_UnNE_JNE = focparams->C_expE_UnNE_JNE_ST;
    double *C_expE_UnE_JE   = focparams->C_expE_UnE_JE_ST;
#endif
    
#if SEA == 1
    double *expE_WWNE_JNEi = focparams->expE_WWNE_JNEi_ST;
    double *expE_WWE_JEi   = focparams->expE_WWE_JEi_ST;
    double *expE_UiNE_JNEi = focparams->expE_UiNE_JNEi_ST;
    double *expE_UiE_JEi   = focparams->expE_UiE_JEi_ST;
#if OPT_compute_C_flow == 1
    double *C_expE_WWNE_JNEi = focparams->C_expE_WWNE_JNEi_ST;
    double *C_expE_WWE_JEi   = focparams->C_expE_WWE_JEi_ST;
    double *C_expE_UiNE_JNEi = focparams->C_expE_UiNE_JNEi_ST;
    double *C_expE_UiE_JEi   = focparams->C_expE_UiE_JEi_ST;
#endif
#endif
    
    int igridCOH=focparams->igridCOHST; int tgridfoc=focparams->tgridST; int zgridfoc=focparams->zgridST;
    int Jinsured=focparams->JinsuredST; // this is the insurance status next period.
    
    double cons, xgrid, dxgrid, valfoc, searcheffW;
    double tempUnNE_JNE, tempWWNE_JNE, tempUnE_JE, tempWWE_JE, tempUiNE_JNEi, tempUiE_JEi, tempWWE_JEi, tempWWNE_JNEi;
    double C_tempUnNE_JNE, C_tempWWNE_JNE, C_tempUnE_JE, C_tempWWE_JE, C_tempUiNE_JNEi, C_tempUiE_JEi, C_tempWWE_JEi, C_tempWWNE_JNEi;
    double diffValue, diffgrid, ddiffgrid;
    int iverif, ixgrid, inxE,inxE1, idiffgrid;
    
    
    // CONSUMPTION //
    cons = findconsRB(xval, gridCOH[igridCOH]);
    
    // ON THE GRID REDEFINITION OF SAVING//
    xgrid  = invexpspace(xval,Gridmin,Gridmax,Echelle1,maxgrid);
    dxgrid = weightinter(xval, xgrid, &ixgrid, grid, maxgrid);
    if ((xval>(Gridmax))){printf("focEhat: (xval>(Gridmax)) %20.15f\n",xval);getchar();}
    if ((ixgrid>=(maxgrid-1))){printf("focEhat: ixgrid>=(maxgrid-1) %d\n",ixgrid);getchar();}
    if ((cons<=0.0)){printf("focEhat: consoFOC<0.0 %20.15f\t%20.15f\t%20.15f\n",xval,cons, findconsRB(0.0, gridCOH[igridCOH]));getchar();}
    if ((xval<(0.0))){printf("focEhat_1: (xval<(0.0)) %20.15f\n",xval);getchar();}
    
    
    // CONTINUATION VALUE //
    inxE  = inxE(ixgrid,tgridfoc,zgridfoc);
    inxE1 = inxE((ixgrid+1),tgridfoc,zgridfoc);
    
    tempWWNE_JNE = inter1d(dxgrid,expE_WWNE_JNE[inxE],expE_WWNE_JNE[inxE1]);
    tempWWE_JE   = inter1d(dxgrid,expE_WWE_JE[inxE],expE_WWE_JE[inxE1]);
    tempUnNE_JNE = inter1d(dxgrid,expE_UnNE_JNE[inxE],expE_UnNE_JNE[inxE1]);
    tempUnE_JE   = inter1d(dxgrid,expE_UnE_JE[inxE],expE_UnE_JE[inxE1]);
    
#if OPT_compute_C_flow == 1
    C_tempWWNE_JNE = inter1d(dxgrid,C_expE_WWNE_JNE[inxE],C_expE_WWNE_JNE[inxE1]);
    C_tempWWE_JE   = inter1d(dxgrid,C_expE_WWE_JE[inxE],C_expE_WWE_JE[inxE1]);
    C_tempUnNE_JNE = inter1d(dxgrid,C_expE_UnNE_JNE[inxE],C_expE_UnNE_JNE[inxE1]);
    C_tempUnE_JE   = inter1d(dxgrid,C_expE_UnE_JE[inxE],C_expE_UnE_JE[inxE1]);
#endif
    
#if SEA == 1
    if(Jinsured == 1){
        tempWWNE_JNEi = inter1d(dxgrid,expE_WWNE_JNEi[inxE],expE_WWNE_JNEi[inxE1]);
        tempWWE_JEi   = inter1d(dxgrid,expE_WWE_JEi[inxE],expE_WWE_JEi[inxE1]);
        tempUiNE_JNEi = inter1d(dxgrid,expE_UiNE_JNEi[inxE],expE_UiNE_JNEi[inxE1]);
        tempUiE_JEi   = inter1d(dxgrid,expE_UiE_JEi[inxE],expE_UiE_JEi[inxE1]);
#if OPT_compute_C_flow == 1
        C_tempWWNE_JNEi = inter1d(dxgrid,C_expE_WWNE_JNEi[inxE],C_expE_WWNE_JNEi[inxE1]);
        C_tempWWE_JEi   = inter1d(dxgrid,C_expE_WWE_JEi[inxE],C_expE_WWE_JEi[inxE1]);
        C_tempUiNE_JNEi = inter1d(dxgrid,C_expE_UiNE_JNEi[inxE],C_expE_UiNE_JNEi[inxE1]);
        C_tempUiE_JEi   = inter1d(dxgrid,C_expE_UiE_JEi[inxE],C_expE_UiE_JEi[inxE1]);
#endif
    }
#endif
    
    
    // FIND THE OPTIMAL EFFORT LEVEL
    if(Jinsured == 0){diffValue = max(0.000000000001,(betapar*((1.0 - phipar)*(tempWWE_JE - tempUnE_JE) + phipar*(tempWWNE_JNE - tempUnNE_JNE))));}
#if SEA == 1
    if(Jinsured == 1){diffValue = max(0.000000000001,(betapar*((1.0 - phipar)*(tempWWE_JEi - tempUiE_JEi) + (phipar)*(tempWWNE_JNEi - tempUiNE_JNEi))));}
#endif
    diffgrid  = invexpspace(diffValue,gridminsearch,gridmaxsearch,EchelleW,maxsearch);
    ddiffgrid = weightinter(diffValue,diffgrid,&idiffgrid,diffvalues,maxsearch);
    searcheffW = inter1d(ddiffgrid,search1D_W[inxS(idiffgrid, tgridfoc)],search1D_W[inxS(idiffgrid+1, tgridfoc)]);
    focparams->searchWoutST=searcheffW;
    
    
    // VALUE FUNCTION
    if(Jinsured == 0){
        valfoc = utilc(cons,1) + disutilityW(searcheffW) + betapar*((1.0 - phipar)*(piW(searcheffW)*tempWWE_JE   + (1.0-piW(searcheffW))*tempUnE_JE) +
                                                                    phipar*(piW(searcheffW)*tempWWNE_JNE + (1.0-piW(searcheffW))*tempUnNE_JNE));
#if OPT_compute_C_flow == 1
        focparams->C_val = utilc(cons,1) + betapar*((1.0 - phipar)*(piW(searcheffW)*C_tempWWE_JE   + (1.0-piW(searcheffW))*C_tempUnE_JE) +
                                                    phipar*(piW(searcheffW)*C_tempWWNE_JNE + (1.0-piW(searcheffW))*C_tempUnNE_JNE));
#endif
    }
    
#if SEA == 1
    if(Jinsured == 1) {
        valfoc = utilc(cons,1) + disutilityW(searcheffW) + betapar*(
                                                                    (1.0 - phipar)*(piW(searcheffW)*tempWWE_JEi   + (1.0-piW(searcheffW))*tempUiE_JEi) +
                                                                    (phipar)*(piW(searcheffW)*tempWWNE_JNEi + (1.0-piW(searcheffW))*tempUiNE_JNEi));
#if OPT_compute_C_flow == 1
        focparams->C_val = utilc(cons,1) + betapar*(
                                                    (1.0 - phipar)*(piW(searcheffW)*C_tempWWE_JEi   + (1.0-piW(searcheffW))*C_tempUiE_JEi) +
                                                    (phipar)*(piW(searcheffW)*C_tempWWNE_JNEi + (1.0-piW(searcheffW))*C_tempUiNE_JNEi));
#endif
    }
#endif
    
    
    valfoc = -valfoc;
    return valfoc;
}




